package com.yiren.dbaa.module.practice_set.map.model;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.currency.entity.MapEntity;
import com.yiren.backstage.currency.utils.DateUtil;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.map.bean.LineBindBean;
import com.yiren.dbaa.module.practice_set.map.contract.MapReleaseContract;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.bean.TestBaseBean;
import com.yiren.dbaa.net.interf.StringCallback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lwb
 * @date : 2022/11/30 14:01
 * @description: 地图发布  车辆模型
 */
public class MapReleaseModel implements MapReleaseContract.Model {

    private Activity activity;
    private Gson gson;

    public MapReleaseModel(Activity activity) {
        this.activity = activity;
        gson = new Gson();
    }

    @Override
    public void publishMap(MapEntity mapEntity, int userId, CommonCallBack callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", mapEntity.getId());
        map.put("siteCode", mapEntity.getSiteCode());
        map.put("name", mapEntity.getName());
        map.put("userId", userId);
        map.put("mapVer", mapEntity.getMapVerLast() + 1);
        map.put("useRange", mapEntity.getUseRange());
        map.put("updateDetails", mapEntity.getUpdateDetails());
        map.put("updateMode", mapEntity.getUpdateModel());
        map.put("publishTime", DateUtil.getCurrentDate());
        map.put("state", 1);
        map.put("baseId", mapEntity.getBaseId());
        map.put("mapType", mapEntity.getMapType());

        new Thread(new Runnable() {
            @Override
            public void run() {
                String line = OkHttpUtils.getInstance().z(AddressConstant.publishMap().getUrl(), new Gson().toJson(map));
                if (!StringUtil.isEmpty(line)) {
                    TestBaseBean<String> testBaseBean = gson.fromJson(line, new TypeToken<TestBaseBean<String>>() {
                    }.getType());
                    if (testBaseBean.isSuccess()) {
                        callBack.success(testBaseBean.getData());
                    } else {
                        callBack.fail("发布失败!");
                    }
                } else {
                    callBack.fail("发布失败!");
                }
            }
        }).start();
    }

    @Override
    public void updateMapBindLines(String mapId, List<LineBindBean> lines, CommonCallBack callBack) {
        for (LineBindBean lineBind : lines) {
            OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.updateMapBindLine("?mapId=" + (lineBind.isBindStatus() ? mapId : "") + "&lineId=" + lineBind.getLineInfo().getId() + "&lineVer=" + lineBind.getLineInfo().getLineVer()), new StringCallback() {
                @Override
                public void getString(String result) {
                    TestBaseBean testBaseBean = gson.fromJson(result, TestBaseBean.class);
                    if (testBaseBean.isSuccess()) {
                        callBack.success("");
                    } else {
                        callBack.fail("添加绑定线路失败");
                    }
                }
            });
        }
    }
}
